<template>
  <div class="onlineMessage">
    <styleConfig
            v-show="currentView === 'styleConfig'"
            :changeWindow="changeWindow"
            :scheduleEnable="scheduleEnable"
            @tellSave="tellSave"
            @backTop="backTop"
            :basicData="onlineMessage"
            @changeWin="changeWin"
            @editStyle="editStyle"
            @editInviteStyle="editInviteStyle"
            @saveConfig="saveConfig"
            ref="childBasic"
    ></styleConfig>
    <!--<invite-box-style v-show="currentView === 'inviteBox'" :styleData="onlineMessage"  ref="inviteBoxStyle" @save="tellSave"></invite-box-style>-->
  </div>
</template>
<script>
  import styleConfig from './styleConfig'
//  import inviteBoxStyle from './inviteBoxStyle.vue'
  import * as uuid from 'uuid'
  export default {
    name: 'ChannelSkillGroup',
    data () {
      return {
        onlineMessage: {},
        isEditWinStyle: false,
        changeWindow: 'pure',
        currentView: 'styleConfig',
        scheduleEnable: false
      }
    },
    props: {
      configMessage: {
        type: Object,
        default: function () {
          return {}
        }
      }
    },
    components: {
      styleConfig
    },
    methods: {
      backToBasic () {
        this.currentView = 'styleConfig'
      },
      backTop () {
        if (this.currentView !== 'styleConfig') {
          this.$emit('getWebchatList')
          this.currentView = 'styleConfig'
        }
      },
      tellSave () {
        let childBasic = this.$refs.childBasic
        childBasic.saveConfig(childBasic.form)
      },
      editStyle (data) {
        this.changeWindow = data.changeWindow
        this.currentView = 'styleConfig'
        this.scheduleEnable = data.scheduleEnable
      },
      changeWin (data) {
        this.changeWindow = data.changeWindow
      },
      editInviteStyle () {
        this.currentView = 'inviteBox'
        this.$refs.inviteBoxStyle.initPreviewWidth()
        this.$emit('onlineMsgBre', '邀请框样式设置')
      },
      // 接受basicSetting子组件传过来的数据，通过$ref获取styleConfig里面的信息
      // 进行判断、处理、保存
      saveConfig (data) {
        if (!data.companyName) {
          this.currentView = 'styleConfig'
          this.$message.error('请您设置商户简称')
          return
        }
        data.platform = 'web'
        data.serviceStates = 1
        if (data._id) {
          this.$store.dispatch('updateEcommerceWebchatCfg', data).then((res) => {
            if (res && res.success) {
              this.$message({
                message: '修改网站接入成功',
                type: 'success'
              })
              this.$emit('getWebchatList')
            }
          })
        } else {
          data.accessId = uuid.v1()
          this.$store.dispatch('addEcommerceWebchatCfg', data).then((rep) => {
            if (rep.success) {
              this.$message({
                message: '添加网站接入成功',
                type: 'success'
              })
              this.$emit('getWebchatList', 'isAdd')
            }
          })
        }
        this.isEditWinStyle = false
      }
    },
    // 监测configMessage 确保这个数据是保持最新的
    watch: {
      configMessage (val) {
        this.currentView = 'styleConfig'
        this.onlineMessage = val
      }
    },
    mounted () {
      this.currentView = 'styleConfig'
    },
    beforeMount () {
      this.isEditWinStyle = false
      this.onlineMessage = this.configMessage
    }
  }
</script>
<style lang="stylus" scoped>
</style>
